﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:SelectCourse
	/// </summary>
	public partial class SelectCourse
	{
		public SelectCourse()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string TC_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SelectCourse");
			strSql.Append(" where TC_Id='"+TC_Id+"' and S_Id='"+S_Id+"' ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

        //判断时间是否能够选课
        public bool TimeIsFree(string S_Id, string TC_Date_Day,string TC_Date_Time )
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SelectCourse,TeachingClass ");
            strSql.Append("where SelectCourse.S_Id= '" + S_Id + "' and " + "SelectCourse.TC_Id=TeachingClass.TC_Id and TeachingClass.TC_Date_Day= '" + TC_Date_Day + "'" + " and TeachingClass.TC_Date_Time= '"+TC_Date_Time+"'");
            return DbHelperSQL.Exists(strSql.ToString());
        
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.SelectCourse model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.TC_Id != null)
			{
				strSql1.Append("TC_Id,");
				strSql2.Append("'"+model.TC_Id+"',");
			}
			if (model.S_Id != null)
			{
				strSql1.Append("S_Id,");
				strSql2.Append("'"+model.S_Id+"',");
			}
			strSql.Append("insert into SelectCourse(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.SelectCourse model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SelectCourse set ");
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where TC_Id='"+ model.TC_Id+"' and S_Id='"+ model.S_Id+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        /// 

        public bool DeleteChoseCourse(string TC_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SelectCourse ");
            strSql.Append(" where TC_Id='" + TC_Id + "'");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        
        
        
        }
        


		public bool Delete(string TC_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SelectCourse ");
			strSql.Append(" where TC_Id='"+TC_Id+"' and S_Id='"+S_Id+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.SelectCourse GetModel(string TC_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" TC_Id,S_Id ");
			strSql.Append(" from SelectCourse ");
			strSql.Append(" where TC_Id='"+TC_Id+"' and S_Id='"+S_Id+"' " );
			Maticsoft.Model.SelectCourse model=new Maticsoft.Model.SelectCourse();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.SelectCourse DataRowToModel(DataRow row)
		{
			Maticsoft.Model.SelectCourse model=new Maticsoft.Model.SelectCourse();
			if (row != null)
			{
				if(row["TC_Id"]!=null)
				{
					model.TC_Id=row["TC_Id"].ToString();
				}
				if(row["S_Id"]!=null)
				{
					model.S_Id=row["S_Id"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        /// 

        public DataSet GetCourseInform(string stdname)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append("FROM TeachingClass,SelectCourse");
            if (stdname.Trim() != "")
            {
                strSql.Append(" where SelectCourse.TC_Id=TeachingClass.TC_Id and SelectCourse.S_Id='"+stdname+"' ");
            }
            return DbHelperSQL.Query(strSql.ToString());
        
        
        
        }


        public DataSet GetCourse(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TeachingClass.TC_Id,TC_Name,Teacher.T_Id,T_Name,S_Id ");
            strSql.Append(" FROM SelectCourse,TeachingClass,Teacher ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TC_Id,S_Id ");
			strSql.Append(" FROM SelectCourse ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" TC_Id,S_Id ");
			strSql.Append(" FROM SelectCourse ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SelectCourse ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.S_Id desc");
			}
			strSql.Append(")AS Row, T.*  from SelectCourse T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

